<?php

namespace App\Admin\Actions\CollectionAction;

use App\Models\User;
use Brick\Math\RoundingMode;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Actions\RowAction;
use App\Supports\Withdraw\Withdrawer;
use Illuminate\Database\Eloquent\Model;
use App\Models\Collection;
use App\Models\CollectionImage;
use App\Models\GcUserImage;
use App\Models\GcText;
use App\Models\GcCategory;
use Carbon\Carbon;

class DistributeGroupImage extends RowAction
{
	public $name = '分配图文';

	public function handle(Model $model, Request $request)
	{
		/*if (!in_array(Admin::user()->id, [1,3])) {
			return $this->response()->error('无权操作')->refresh();
		}*/

		/**
		 * @var User $user
		 */

		
		$gcType = $request->get('gc_type', 0);

		if (!in_array($gcType, ['xhs', 'dy','sph','ks'])) {
			return $this->response()->error('请选择渠道');
		}

		$gcCategoryId = $request->get('gc_category_id', 0);
		if(empty($gcCategoryId)){

			return $this->response()->error('请选择分类');
		}

		$number = $request->get('number', 0);
		if(empty($number)){

			return $this->response()->error('请填写数量');
		}

		

		$arr = [];
		$collection = Collection::query()->where('gc_category_id',$gcCategoryId)->where('gc_type',$gcType)->get();
		$texts = GcText::query()->where('gc_category_id',$gcCategoryId)->where('gc_type',$gcType)->get();



		if(empty($collection->toArray())){
			return $this->response()->error('该渠道该类型无合成图');

		}
		$textArr = [];
		foreach($texts as $k=>$v){
			$textArr[] = $v;
				

		}


		foreach($collection as $k=>$v){

			$rr = $v->collectionImages()->where('is_used',false)->get()->toArray();
			
			if(empty($rr)){

				return $this->response()->error('该渠道该类型合成图组可用数量不足');
			}
			$arr[] = $rr;

		}

		

		$insert = [];
		$images = [];
		$collectionImagesUpdateIdArr = [];
		$minLength = count(min($arr));
		$allTextLength = count($textArr);



		if($number > $minLength){

			return $this->response()->error('该渠道最大合成图数为'.$minLength);
		}
		

		foreach($arr as $k=>$v){
				
			
			for($j=0;$j<$number;$j++){

				$images[$j][] = $v[$j]['image'];
				$collectionImagesUpdateIdArr[] = $v[$j]['id'];
			}

		}



		
		$insert = [];
		foreach($images as $ik=>$iv){
			$rand = 0;
			if($allTextLength> 0){
				$rand = rand(0,$allTextLength-1);
			}


			$insert[$ik]=[
				'gc_group_id'=>$model->id,
				'gc_type'=>$gcType,
				'gc_category_id'=>$gcCategoryId,
				'images' => collect($iv),
				'title_main' => $allTextLength > 0 ? $textArr[$rand]->title : '',
				'content_main' => $allTextLength > 0 ? $textArr[$rand]->content : '',
				'created_at'=>Carbon::now()
			];



		}

		CollectionImage::whereIn('id',$collectionImagesUpdateIdArr)->update(['is_used'=>true]);

		GcUserImage::insert($insert);


		return $this->response()->success('图片已分配至分配管理系统中')->refresh();
	}

	public function form()
	{
		$this->select('gc_type', '渠道')
            ->options(config("site.gcType"));
        $this->select('gc_category_id', '分类')
            ->options(GcCategory::get()->pluck('name', 'id'));
       $this->text('number', '数量')
			->rules('required|numeric|min:0.01');

		
	}
}
